#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

struct stack *
create_stack(void)
{
    struct stack *s = NULL;
    if ((s = (struct stack *)malloc(sizeof(struct stack))) != NULL)
    {
        s->element_num = 0;
        s->node = NULL;
    }
    return (s);
}

void
destroy_stack(struct stack *s)
{
    if (s != NULL)
    {
        destroy_list(s->node);
        free(s);
        s = NULL;
    }
}

int
push(struct stack *s, const void *value, int size)
{
    if (s != NULL)
    {
        struct list *new_node = NULL;
        if ((new_node = create_list(new_node, value, size)) != NULL)
        {
            new_node->next = s->node;
            s->node = new_node;
            s->element_num++;
            return (0);
        }
    }
    return (-1);
}

void
pop(struct stack *s)
{
    if (s!= NULL && s->node != NULL)
    {
        struct list *free_list = NULL;
        free_list = s->node;
        s->node = free_list->next;
        free_list->next = NULL;
        destroy_list(free_list);
        free_list = NULL;
        s->element_num--;
    }
}

const void *
top(const struct stack *s)
{
    if (s!= NULL && s->node != NULL)
    {
        return (s->node->value);
    }
    return (NULL);
}

int
is_empty(const struct stack *s)
{
   if (s != NULL && s->node != NULL)
   {
       return (0);
   }
   return (1);
}

int
is_full(const struct stack *s)
{
    if (s != NULL)
    {
        return (0);
    }
    return (1);
}


int
get_element_num(const struct stack *s)
{
    return (s->element_num);
}
